/**
 * 添加好友列表
 */
class friendAdd extends eui.Component{
    private return:eui.Button;
    private numId:eui.EditableText;
    private search:eui.Image;
    private refresh:eui.Image;
    private note:eui.Label;
    private addFriend:eui.Button;

    private list:eui.List;

    private _arrayData:eui.ArrayCollection;
    private _friendData:Array<any> = [];

    private _page:number = 0;
    private _mpage:number = 0;

    private _userId:number = 0;

    private _isAdd = 0;
    public constructor(){
        super();
       // this.cacheAsBitmap =true;
        this.addEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent,this);
        this.skinName = "friendAddSkin"; 
    }

    private createCompleteEvent():void{
        this.removeEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent, this);
        this.return.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            this.parent.dispatchEventWith("close",true,this._isAdd);
            this._isAdd = 0;
        },this);
        this.inIt();
    }

    private inIt(){
        egret.TextFieldInputType.TEL;
        
        this.search.addEventListener(egret.TouchEvent.TOUCH_TAP,this.mbtnHandler,this);
        this.refresh.addEventListener(egret.TouchEvent.TOUCH_TAP,this.mbtnHandler,this);
        this.numId.addEventListener("change",this.mbtnHandler,this);
        this.addFriend.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            this.parent.dispatchEventWith("close",true,this._isAdd);
            this._isAdd = 0;
        },this);

        Dispatcher.ins.addEventListener("addFriendList",this.assignValue,this);
        Dispatcher.ins.addEventListener("addFriend",()=>{
            let toast = new Toast();
            toast.show("添加好友成功");
            this.addChild(toast);
            this.getList();   
            this._isAdd = 1;
        },this);
        Dispatcher.ins.addEventListener("searchFriend",this.assignValue,this);

        this._arrayData = new eui.ArrayCollection(this._friendData);
        this.list.dataProvider = this._arrayData;
        this.list.itemRenderer = friendAddIR;
        this.getList();
    }
    public getList():void{
        let dataPost = {"page":this._page};
        socket.request("addFriendList",dataPost);
    }
    public assignValue(e: GameEvent){
        let obj = e.data;
        let list = obj.list;
        this._friendData = list;
        this._arrayData = new eui.ArrayCollection(this._friendData);
        this.list.dataProvider = this._arrayData;
        this._arrayData.refresh();
        this._page = this._page + 1;
    }
    private mbtnHandler(evt:egret.TouchEvent):void{
        evt.stopPropagation();
        let btn = evt.currentTarget;
        switch(btn){
            case this.numId:{
                let value = this.numId.text;
                value = value.replace(/[^0-9]/g,'');
                this.numId.text = value;
                if(value.length == 0){
                    this.note.visible = true;
                }else{
                    this.note.visible = false;
                }
                break;
            }
            case this.search:{
                if(this.numId.text.length == 0){
                    let toast = new Toast();
                    toast.show("请输入查找的用户id");
                    this.addChild(toast);
                }else{
                    let dataPost = {"toUserId":parseInt(this.numId.text)};
                    socket.request("searchFriend",dataPost);
                }
                break;
            }
            case this.refresh:{
                this.numId.text = "";
                this.note.visible = true;
                this.getList();
                break;
            }
        }
    }
}
class friendAddIR extends eui.ItemRenderer{
    private maskBg:eui.Rect;
    private head:eui.Image;
    private sex:eui.Image;
    private add:eui.Image;
    // private changeBtn:eui.Button;
    public constructor(){
        super();
        this.addEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent,this);
        this.skinName = "friendAddIRSkin";
    }
    private createCompleteEvent():void{
        super.createChildren();
        this.removeEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent, this);
        this.head.mask = this.maskBg;
        this.add.addEventListener(egret.TouchEvent.TOUCH_TAP,(evt:egret.TouchEvent)=>{
            evt.stopPropagation();
            let dataPost = {"addUserId":this.data.UserId}
            socket.request("addFriend",dataPost);
        },this);
        let status = "";
        this.addEventListener(egret.TouchEvent.TOUCH_BEGIN,()=>{
            status = "begin";
        },this);
        this.addEventListener(egret.TouchEvent.TOUCH_MOVE,()=>{
            status = "move";
        },this);
        this.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            if(status !== "move"){
                Dispatcher.ins.dispatchEvent(new GameEvent("openOtherHome",{"isFriend":1,"userId":this.data.UserId}));
            }
        },this);
    }
    protected dataChanged():void{
        if(this.data.Sex == 1){
            this.sex.source = "icon_friend_male_png";
        }else{
            this.sex.source = "icon_friend_female_png";
        }
    }
}